import java.io.InputStream;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;


public class ExGameCanvas extends Canvas implements Runnable{
private boolean isPlay; // Game Loop runs when isPlay is true
	private long delay; // To give thread consistency
	public int width; // To hold screen width
	public int height; // To hold screen height	
	
	public static Mscreen game_src, Mquaman , logo , menu , help , restMap , overMap;		

	public static Mscreen currentSreen;
	public static boolean keyHold[] = new boolean[21];



	public static boolean keyPressed[] = new boolean[21];
	public static boolean isMoto;

	public ExGameCanvas() {
		Mscreen.widthScreen = width = getWidth();
		Mscreen.heightScree = height = getHeight();
		delay = 20;
		game_src = new Game_src();
		Mquaman = new MaddMap();
		restMap = new MResetMap();
		overMap = new MOverMap();
		menu = new Mmenu();
		logo = new Mlogo();
		help = new Mhelp();
		logo.currentSreen();

	}
	

	public void start() {
		isPlay = true;
		Thread t = new Thread(this);
		t.start();
	}

	public void stop() {
		isPlay = false;
	}

	public void run() {
		// TODO Auto-generated method stub
		while (isPlay == true) 
		{			
			update();								
			repaint();
			try {				
				Thread.sleep(delay);
			} catch (InterruptedException ie) {
			}
		}
	}
	
	protected void keyPressed(int keyCode) {
		switch (keyCode) {
		case 'e':// number 2 fmobile (E)
		case 'E':
			keyHold[2] = true;
			keyPressed[2] = true;
			return;
			// case 88:// number 8 fmobile (X)
		case 'x':
		case 'X':
			keyHold[8] = true;
			keyPressed[8] = true;
			return;
			// case 83:// number 4 fmobile (S)
		case 's':
		case 'S':
			keyHold[4] = true;
			keyPressed[4] = true;
			return;
			// case 70:// number 6 fmobile (F)
		case 'f':
		case 'F':
			keyHold[6] = true;
			keyPressed[6] = true;
			return;
			// case 68:// number 5 fmobile (D)
		case 'd':
		case 'D':
			keyHold[5] = true;
			keyPressed[5] = true;
			return;
		case 'w':
		case 'W':
			keyHold[1] = true;
			keyPressed[1] = true;
			return;
		case 'r':
		case 'R':
			keyHold[3] = true;
			keyPressed[3] = true;
			return;
		case 'z':
		case 'Z':
			keyHold[7] = true;
			keyPressed[7] = true;
			return;
		case 'c':
		case 'C':
			keyHold[9] = true;
			keyPressed[9] = true;
			return;
		}
		if (isMoto) {
			switch (getGameAction(keyCode)) {
			case UP:
				keyHold[2] = true;
				keyPressed[2] = true;
				return;
			case DOWN:
				keyHold[8] = true;
				keyPressed[8] = true;
				return;
			case LEFT:
				keyHold[4] = true;
				keyPressed[4] = true;
				return;
			case RIGHT:
				keyHold[6] = true;
				keyPressed[6] = true;
				return;
			case FIRE:
				keyHold[5] = true;
				keyPressed[5] = true;
				return;
			}
			if (keyCode == -21) // Soft 1
				keyCode = -6;
			else if (keyCode == -22) // Soft 2
				keyCode = -7;
		}
		switch (keyCode) {
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			keyHold[keyCode - 48] = true;
			keyPressed[keyCode - 48] = true;
			return; // 0 -> 9
		case 42:
			keyHold[10] = true;
			return; // Key [*]
		case 35:
			keyHold[11] = true;
			return; // Key [#]
		case -6:
			keyHold[12] = true;
			keyPressed[12] = true;
			return; // Soft1
		case -7:
			keyHold[13] = true;
			keyPressed[13] = true;
			return; // Soft2
		case -5:
			keyHold[5] = true;
			keyPressed[5] = true;
			return; // [i]
		}
		switch (getGameAction(keyCode)) {
		case UP:
			keyHold[2] = true;
			keyPressed[2] = true;
			return;
		case DOWN:
			keyHold[8] = true;
			keyPressed[8] = true;
			return;
		case LEFT:
			keyHold[4] = true;
			keyPressed[4] = true;
			return;
		case RIGHT:
			keyHold[6] = true;
			keyPressed[6] = true;
			return;
		case FIRE:
			keyHold[5] = true;
			keyPressed[5] = true;
			return;
		}
	}
	

	protected void keyReleased(int keyCode) {
		switch (keyCode) {
		case 'e':// number 2 fmobile (E)
		case 'E':
			keyHold[2] = false;
			return;
			// case 88:// number 8 fmobile (X)
		case 'x':
		case 'X':
			keyHold[8] = false;
			return;
			// case 83:// number 4 fmobile (S)
		case 's':
		case 'S':
			keyHold[4] = false;
			return;
			// case 70:// number 6 fmobile (F)
		case 'f':
		case 'F':
			keyHold[6] = false;
			return;
			// case 68:// number 5 fmobile (D)
		case 'd':
		case 'D':
			keyHold[5] = false;
			return;
		case 'w':
		case 'W':
			keyHold[1] = false;
			return;
		case 'r':
		case 'R':
			keyHold[3] = true;
			return;
		case 'z':
		case 'Z':
			keyHold[7] = true;
			return;
		case 'c':
		case 'C':
			keyHold[9] = true;
			return;
		}
		if (isMoto) {
			switch (getGameAction(keyCode)) {
			case UP:
				keyHold[2] = false;
				return;
			case DOWN:
				keyHold[8] = false;
				return;
			case LEFT:
				keyHold[4] = false;
				return;
			case RIGHT:
				keyHold[6] = false;
				return;
			case FIRE:
				keyHold[5] = false;
				return;
			}
			if (keyCode == -21) // Soft 1
				keyCode = -6;
			else if (keyCode == -22) // Soft 2
				keyCode = -7;
		}
		switch (keyCode) {
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			keyHold[keyCode - 48] = false;
			return; // 0 -> 9
		case 42:
			keyHold[10] = false;
			return; // Key [*]
		case 35:
			keyHold[11] = false;
			return; // Key [#]
		case -6:
			keyHold[12] = false;
			return; // Soft1
		case -7:
			keyHold[13] = false;
			return; // Soft2
		case -5:
			keyHold[5] = false;
			return; // [i]
		}
		switch (getGameAction(keyCode)) {
		case UP:
			keyHold[2] = false;
			return;
		case DOWN:
			keyHold[8] = false;
			return;
		case LEFT:
			keyHold[4] = false;
			return;
		case RIGHT:
			keyHold[6] = false;
			return;
		case FIRE:
			keyHold[5] = false;
			return;
		}
	}

	public static void clear_key() {
		for (int i = 0; i < 21; i++) {
			keyHold[i] = false;
			keyPressed[i] = false;
		}
	}

	public static void clear_keypressed() {
		for (int i = 0; i < 21; i++) {
			keyPressed[i] = false;
		}
	}

	public void update()
	{
		currentSreen.update();
	}


	protected void paint(Graphics g) {
		// TODO Auto-generated method stub
		
		g.setColor(0xFFFFFF);	
		g.fillRect(0, 0, getWidth(), getHeight());
		currentSreen.paint(g);
	}
}
